// pages/focus/index.js
var index = 0
var util = require('../../utils/util');
var BASE_URL = getApp().globalData.BASE_URL

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    id: '',
    questionList: '', //精选问题列表
    showloading: true, //加载中loading
    showfinish: false, //数据加载完成loading
    flag: false,
    tuijainlist: '',
    articleList:'',
    zuozelist:'',
    page: 2,
    num: null,
    cate:'推荐'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
  
 //推荐的文章

 wx.request({
  url: `${BASE_URL}/ajax/jinri?page=1&size=10`,
  method: "POST",
  header: {
    'Content-type': 'application/x-www-form-urlencoded'
    // 'Content-Type': 'application/json'
  },
  success(res) {
    res.data.row.forEach(function (item, index) {
      if (item.other_info) {
        item.other_info = ''
      }
    })
    
    console.log(res,1111)
    _this.setData({
      tuijainlist:res.data.row,
      showloading:false,
      num:res.data.num
    })
  }
})

//推荐朋友
    wx.request({
      url: `${BASE_URL}/ajax/redispublic/type/zuozhe`,
      method: "POST",
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
        // 'Content-Type': 'application/json'
      },
      success(res) {
        res.data.forEach(item => {
          item.forEach(itemm => {
            if (itemm.avatar.length < '60' && itemm.avatar.length !== 0) {
              itemm.avatar = `${BASE_URL}/` + itemm.avatar
            }
            if (itemm.avatar.length == 0) {
              itemm.avatar = `${BASE_URL}/uploads/touxiang/m_5d54f81ee9739.gif`
            }
          })
        })
        _this.setData({
          zuoze: res.data,
          zuozelist: res.data[0]
        })
      }
    }),


    //好友的文章
    wx.request({
      url: `${BASE_URL}/ajax/wenzhang/cate/${"新闻"}`,
      method: "GET",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        res.data.data.forEach(function (item, index) {
          if (item.banner) {
            item.cover = item.banner
          }
        })
        _this.setData({
          articleList: res.data.data,
          // cate: '热点',
          num: res.data.num
        })
      }
    })

  },

  onShow: function () {
    if (util.iflogin()) {
      console.log(1111)
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        flag: true,
        id: userinfo.id
      })
    }
  },


  //换一换
  Inabatch: function () {
    index = index + 1
    if (index == 4) {
      index = 0
    }
    this.setData({
      zuozelist: this.data.zuoze[index]
    })
  },
  //关注
  guanzhu: function (e) {
    var _this = this
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: `${BASE_URL}/user/dianguanzhu/id/${e.currentTarget.dataset.id }/uid/${_this.data.id}`,
      method: "GET",
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
        // 'Content-Type': 'application/json'
      },
      success() {
        var info = wx.getStorageSync('USER')
        var opid = wx.getStorageSync('openid')
        var unid = wx.getStorageSync('unionid')

        util.login(info.avatarUrl, info.city, info.country, info.gender, info.language, info.nickName,opid, info.province, unid)
        console.log("你点了关注", info)
        wx.showToast({
          title: '关注成功',
          icon: 'success',
          duration: 1000
        })
      }
    })

    var _this = this;
    wx.request({
      url: `${BASE_URL}/ask`,
      method: "POST",
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
        // 'Content-Type': 'application/json'
      },
      success(res) {
        res.data.data.forEach(function (item, index) {
          item.mess.time = util.formatTimeTwo(item.mess.time, 'Y-M-D h:m:s');
          item.time = util.formatTimeTwo(item.time, 'Y-M-D h:m:s');
        })
        _this.setData({
          questionList: res.data.data,
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },
  onChange(event) {
    console.log(event.detail.title)
  this.setData({
    cate:event.detail.title,
    page:2
  })
  },
  answerDetail: function (e) {
    var id = e.currentTarget.dataset.id //获取点击产品时拿
    wx.navigateTo({
      url: `/views/answerdetail/index?id=${id}`
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: util.throttle(function (e) {
    var _this = this
    console.log(_this.data.articleList.length, _this.data.num)
    if (_this.data.articleList.length + 11 !== _this.data.num) {
      _this.setData({
        page: _this.data.page + 1,
        showloading: true,
        showfinish: false,
      })
    } else {
      _this.setData({
        showloading: false,
        showfinish: true,
      })
    }
    if (_this.data.showloading) {
      if(_this.data.cate=='好友'){
        setTimeout(function () {
          wx.request({
            url: `${BASE_URL}/ajax/wenzhang?page=${_this.data.page}&cate=${'新闻'}`,
            header: {
              'Content-Type': 'application/json', //GET方式是这个
            },
            method: "GET",
            success(res) {
              console.log(res)
              console.log(_this.data.page)
              res.data.data.forEach(function (item, index) {
                if (item.banner) {
                  item.cover = item.banner
                }
              })
              _this.setData({
                showloading: false,
                articleList: _this.data.articleList.concat(res.data.data),
              })
            }
          })
        }, 1000)
      }
      if(_this.data.cate=='推荐'){
        setTimeout(function () {
          wx.request({
            url: `${BASE_URL}/ajax/jinri?page=${_this.data.page}&size=10`,
            header: {
              'Content-Type': 'application/json', //GET方式是这个
            },
            method: "POST",
            success(res) {
              console.log(res)
              console.log(_this.data.page)
              res.data.row.forEach(function (item, index) {
                if (item.other_info) {
                  item.other_info = ''
                }
              })
              _this.setData({
                showloading: false,
                tuijainlist: _this.data.tuijainlist.concat(res.data.row),
              })
            }
          })
        }, 1000)
      }
      
    }
    console.log("触底了")
  }, 2000),

 //跳转文章详情页
 articleDetail: function (e) {
  var id = e.currentTarget.dataset.id 
  wx.navigateTo({
    url: `/views/txtdetail/index?id=${id}`
  })
},
//跳到活动详情页
articlexiangqing: function (e) {
  var id = e.currentTarget.dataset.id
  wx.navigateTo({
    url: `/views/huodongdetail/index?id=${id}`
  })
},

  tologin: function () {
    wx.navigateTo({
      url: `/views/login/index`
    })
  },
  gohomepage: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/views/mine/otherhomepage/index?id=${id}`
    })
  },
})